<template>
  <div>
    <!-- 列表组件 -->
    <ansyertempTab
      v-if="!addansyerts"
      :tableData="receiveInfo"
      :option="optionList"
      @addnewanswer="addnewanswer"
      @getInfodata="getInfodata"
      @handleChange="handleChange"
      @lookdata="lookdata"
      @editinfo="editinfo"
      @deleteRow="deleteRow"
    />

    <!-- 新增答题模板 -->
    <addanswer
      v-if="addansyerts"
      :tableData="answerinfo"
      :option="optionList"
      @goback="goback"
      @storages="storages"
      @saves="saves"
    />
  </div>
</template>

<script>
// import {} from "@/api/";
import ansyertempTab from "@/views/answertemplate/ansyertempTab.vue";
import addanswer from "@/views/answertemplate/addanswer.vue";
import {
  addnewanswertab,
  getnewanswertab,
  setnewanswertab
} from "@/api/hubeixiaojin/queryInfo";
export default {
  components: {
    ansyertempTab,
    addanswer
  },

  data() {
    // 表单身份证
    var idcardReg = /^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$|^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}([0-9]|X)$/;
    var validatePhone = (rule, value, callback) => {
      if (!value) {
        return callback(new Error("证件号不能为空!!"));
      }
      if (!idcardReg.test(value)) {
        callback(new Error("请输入正确的证件号码！"));
      } else {
        callback();
      }
    };
    return {
      addansyerts: false,
      //表头
      optionList: [
        {
          label: "序号",
          prop: "num"
        },
        {
          label: "答题模板名称",
          prop: "name"
        },
        {
          label: "进件通路",
          prop: "incomingPassage"
        },
        {
          label: "状态",
          prop: "status"
        },
        {
          label: "模板总分",
          prop: "totalScore"
        },
        {
          label: "操作人",
          prop: "operator"
        },
        {
          label: "操作时间",
          prop: "createTime"
        }
        // {
        //   label: "操作",
        //   prop: ""
        // }
      ],

      //列表数据
      receiveInfo: {
        list: [],
        paging: true,
        page: {
          total: "",
          pageSize: 10,
          page: 1
        }
      },
      //问题详细信息
      answerinfo: {},

      cxname: "",
      agentInfo: {
        //当前座席名字和id
        // dispatchingPerson: Constants.Agent.agentName,
        // dispatchingId: Constants.Agent.agentId,
        // //目标座席和id
        // agentId: "",
        // agentName: "",
        // //座席行政组
        // deptGroupName: Constants.Agent.deptGroupName,
        // //行政组code
        // deptGroupCode: Constants.Agent.deptGroupCode,
        // //access_token
        // access_token: Constants.Agent.access_token,
        // //tenantId
        // tenantId: Constants.Agent.tenantId,
        // //orgCode
        // orgCode: Constants.Agent.orgCode
      } //调单用到的座席信息
    };
  },
  mounted() {
    const sessionStr = sessionStorage.getItem("userInfo");
    const agentUser = JSON.parse(sessionStr);
    this.agentInfo = agentUser.content;
    this.getInfo();
  },
  methods: {
    //删除按钮
    deleteRow() {
      this.getInfo();
    },
    //查看按钮
    lookdata(val) {
      // console.log("查看按钮", val);
      this.answerinfo = val;
      this.answerinfo.typels = "1";
      this.addansyerts = true;
      console.log("查看传输数据=", this.answerinfo);
    },
    editinfo(val) {
      //编辑按钮
      this.answerinfo = val;
      this.answerinfo.typels = "0";
      this.addansyerts = true;
      console.log("编辑=", this.answerinfo);
    },

    //分页
    handleChange(val) {
      // console.log(`当前页: ${val}`);
      this.receiveInfo.page.page = val;
      this.getInfo();
    },
    getInfodata(val) {
      // console.log("搜索按钮", val);
      this.cxname = val.name;
      this.receiveInfo.page.page = 1;
      this.getInfo();
    },
    addnewanswer() {
      this.answerinfo = {};
      //展开新建答题页面
      this.addansyerts = true;
    },

    //首页数据
    getInfo() {
      var params = {
        condition: {
          submitted: false,
          name: this.cxname
        },
        page: this.receiveInfo.page.page,
        pageSize: this.receiveInfo.page.pageSize,
        sortCol: "id"
      };
      // console.log("调用queryInfofaceuserinfo");
      getnewanswertab(params, this.agentInfo).then(res => {
        res.data.data.listData.forEach((item, index) => {
          item.num = index + 1;
        });

        this.receiveInfo.list = res.data.data.listData;
        this.receiveInfo.page.total = res.data.data.totalCount;
      });
    },

    handleCurrentChange() {
      console.log("触发handleCurrentChange,分页");
    },

    goback() {
      //返回数据搜索页
      this.addansyerts = false;
      this.getInfo();
    },
    storages(val) {
      //暂存按钮
      console.log("接收暂存的数据:", val);

      //获取时间
      let now = new Date();
      let year = now.getFullYear();
      let month = String(now.getMonth() + 1).padStart(2, "0"); // 月份从0开始，所以需要+1
      let day = String(now.getDate()).padStart(2, "0");
      let hours = String(now.getHours()).padStart(2, "0");
      let minutes = String(now.getMinutes()).padStart(2, "0");
      let seconds = String(now.getSeconds()).padStart(2, "0");

      let formattedDateTime = `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;

      //接口暂存   要区分是编辑还是新增

      if (val.typels === "0" || val.typels === 0) {
        //编辑模式
        var params = {
          // sortCol: "id",
          condition: {
            verifyQuestions: val.answerdata, //详细数据

            submitted: false,
            createTime: formattedDateTime,
            incomingPassage: val.incomingPassage, //进件通路
            name: val.name, //问题名称
            remark: val.remark, //模板备注
            totalScore: val.totalScore, //总分
            updateTime: formattedDateTime, //更新时间
            id: val.id, //模板id

            operator: this.agentInfo.dispatchingPerson //当前登录座席
          } //申请状态
        };
        setnewanswertab(params, this.agentInfo).then(res => {
          this.$message.success("编辑问题模板成功");
          this.goback();
        });
      } else {
        //新增
        var params = {
          // sortCol: "id",
          condition: {
            verifyQuestions: val.answerdata, //详细数据

            submitted: false,
            createTime: formattedDateTime,
            incomingPassage: val.incomingPassage, //进件通路
            name: val.name, //问题名称
            remark: val.remark, //模板备注
            totalScore: val.totalScore, //总分
            updateTime: formattedDateTime, //更新时间

            operator: this.agentInfo.dispatchingPerson //当前登录座席
          } //申请状态
        };
        // console.log("调用queryInfofaceuserinfo");
        addnewanswertab(params, this.agentInfo).then(res => {
          this.$message.success("暂存问题模板成功");
          this.goback();
        });
      }
    },
    saves(val) {
      //保存按钮
      console.log("接收保存的数据 typels:", val);
      // console.log("接收保存的数据22 typels:");
      //获取时间
      let now = new Date();
      let year = now.getFullYear();
      let month = String(now.getMonth() + 1).padStart(2, "0"); // 月份从0开始，所以需要+1
      let day = String(now.getDate()).padStart(2, "0");
      let hours = String(now.getHours()).padStart(2, "0");
      let minutes = String(now.getMinutes()).padStart(2, "0");
      let seconds = String(now.getSeconds()).padStart(2, "0");

      let formattedDateTime = `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;

      //接口暂存  要区分是编辑还是新增

      if (val.typels === "0" || val.typels === 0) {
        //编辑模式
        var params = {
          // sortCol: "id",
          condition: {
            verifyQuestions: val.answerdata, //详细数据

            submitted: false,
            createTime: formattedDateTime,
            incomingPassage: val.incomingPassage, //进件通路
            name: val.name, //问题名称
            remark: val.remark, //模板备注
            totalScore: val.totalScore, //总分
            updateTime: formattedDateTime, //更新时间
            id: val.id, //模板id
            operator: this.agentInfo.dispatchingPerson //当前登录座席
          } //申请状态
        };
        setnewanswertab(params, this.agentInfo).then(res => {
          this.$message.success("编辑问题模板成功");
          this.goback();
        });
      } else {
        //新增
        var params = {
          // sortCol: "id",
          condition: {
            verifyQuestions: val.answerdata, //详细数据

            submitted: false,
            createTime: formattedDateTime,
            incomingPassage: val.incomingPassage, //进件通路
            name: val.name, //问题名称
            remark: val.remark, //模板备注
            totalScore: val.totalScore, //总分
            updateTime: formattedDateTime, //更新时间

            operator: this.agentInfo.dispatchingPerson //当前登录座席
          } //申请状态
        };
        // console.log("调用queryInfofaceuserinfo");
        addnewanswertab(params, this.agentInfo).then(res => {
          this.$message.success("新增问题模板成功");
          this.goback();
        });
      }
    }
  }
};
</script>

<style>
.form-inline {
  /* display: flex; */
  /* justify-content: space-between; */
  /* margin-bottom: 20px; */
}
.Search {
  width: 100%;
  text-align: center;
}
.AdjustInput {
  display: flex;
  justify-content: end;
}
</style>
